import {
  batchOptReq,
  consListReq,
  departmentListReq,
  dictionaryAddReq,
  dictionaryDeleteReq,
  dictionaryDetailReq,
  dictionaryEditReq,
  dictionaryGroupListReq,
  dictionaryReleaseReq,
} from './api'

export default {
  namespace: 'standardDefine',
  state: {
    dictionaryList: [],
  },
  effects: {
    // 获取管理列表
    // * getList({ params, cb }, { call, put }) {
    //   const res = yield call(listReq, params)
    //   console.log('res', res)
    // },

    // 字典目录
    * dictionaryGroupList({ params, cb }, { call, put }) {
      const res = yield call(dictionaryGroupListReq, params)
      if (res && cb) {
        cb(res)
      }
      yield put({
        type: 'save',
        payload: {
          dictionaryList: res || [],
        },
      })
    },
    // 获取查看字典详情内的一些常量选项
    * consList({ params, cb }, { call, put }) {
      const res = yield call(consListReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    // 获取归属部门选项
    * departmentList({ params, cb }, { call, put }) {
      const res = yield call(departmentListReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    // 添加标准
    * dictionaryAdd({ params, cb }, { call, put }) {
      const res = yield call(dictionaryAddReq, params)
      if (cb) {
        cb(res)
      }
    },
    // 编辑标准
    * dictionaryEdit({ params, cb }, { call, put }) {
      const res = yield call(dictionaryEditReq, params)
      if (cb) {
        cb(res)
      }
    },
    // 删除标准
    * dictionaryDelete({ params, cb }, { call, put }) {
      const res = yield call(dictionaryDeleteReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    // 发布或下线标准
    * dictionaryRelease({ params, cb }, { call, put }) {
      const res = yield call(dictionaryReleaseReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    // 查看标准详情
    * dictionaryDetail({ params, cb }, { call, put }) {
      const res = yield call(dictionaryDetailReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    // 批量操作
    * batchOpt({ params, cb }, { call, put }) {
      const res = yield call(batchOptReq, params)
      if (cb) {
        cb(res)
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
  },
}
